package com.zmn.oms.task.job.third.dj58;

import com.alibaba.fastjson.JSON;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.spring.tasktracker.JobRunnerItem;
import com.github.ltsopensource.spring.tasktracker.LTS;
import com.github.ltsopensource.tasktracker.Result;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.manager.utils.TaskConsts;
import com.zmn.oms.model.entity.work.OrderWorkLogChange;
import com.zmn.oms.third.dj58.DaoJia58Consts;
import com.zmn.oms.third.dj58.service.DaoJia58Service;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 类描述：58到家直约API渠道同步接单任务，5秒后执行
 *
 * @author xiewenbing
 * @date 2021/08/20 15:40
 */
@LTS
@Slf4j
public class DaoJia58SyncAcceptOrderJob {

    @Autowired
    private DaoJia58Service daoJia58Service;

    /**
     * 同步接单任务
     * @param job
     * @return
     */
    @JobRunnerItem(shardValue = TaskConsts.TASK_ID_58DJ_SYNC_ACCEPT_ORDER_JOB)
    public Result runWorkJob(Job job) {

        if (!DaoJia58Consts.POWER_ON) {
            log.info("【{}】<同步接单> 该功能没有开启。", DaoJia58Consts.CHANNEL_NAME);
            return new Result(Action.EXECUTE_FAILED, "不需要执行!");
        }

        String jobParam = job.getParam("orderWorkLogChange");
        if (StringUtil.isBlank(jobParam)) {
            log.info("【{}】<同步接单> 任务参数为空。", DaoJia58Consts.CHANNEL_NAME);
            return new Result(Action.EXECUTE_FAILED, "任务参数为空!");
        }

        try {
            OrderWorkLogChange orderWorkLogChange = JSON.parseObject(jobParam, OrderWorkLogChange.class);
            // 同步接单
            daoJia58Service.sendAcceptOrderTo58DaoJia(orderWorkLogChange);
        } catch (OmsBaseException e) {
            log.info("【{}】<同步接单>任务异常 --【{}】", DaoJia58Consts.CHANNEL_NAME,e.getMessage());
            return new Result(Action.EXECUTE_FAILED, e.getMessage());
        }

        log.info("#oms#58dj_sync_accept_order_job#同步接单任务");
        return new Result(Action.EXECUTE_SUCCESS, "执行成功了!");
    }
}
